var _page = $(".page");

var moment_lastdate = null;
layui.flow.load({
    elem: '#moment-list' //流加载容器
    // ,scrollElem: '#LAY_demo1' //滚动条所在元素，一般不用填，此处只是演示需要。
    ,isAuto: true
    ,isLazyimg: true
    ,done: function(page, next){ //执行下一页的回调

        var lis = [];
        bottlecms.get("/m/ajax/momen/list", {
            page: page,
            orderBy: $("#orderBy").val()
        }, function(data) {

            layui.each(data.list, function(index, item){
                lis.push(moments_list_template(item));
            });
            next(lis.join(''), lis.length);
        });


    }
});


function moments_list_template(v) {
    var html = '<div class="item"  data-id="' + v.id + '">';
    if (v.single) {
        html += '<div class="userbox"><a class="nick" href="/single/info/' + v.single.userId + '">';
        html += '    <div class="photo"><img src="' + v.single.photo + '_200x200.jpg"></div>';
        html += '    <div class="nickname">' + v.single.nickname + ' <span class="icon gender_' + v.single.gender + '"></span></div>';
        html += '    <div class="userinfo">';
        html += '       <span>' + v.single.age + '岁</span>';
        if (v.single.height) html += '       <span> . </span><span>' + v.single.height + 'cm</span>';
        if (v.single.constellationCn) html += '       <span> . </span><span>' + v.single.constellationCn + '</span>';
        html += '    </div>';
        html += '</a></div>';
    }
    else {
        html += '<div class="ub-f1 singlenull info">用户不存在或已删除～</div>';
    }
    html += '<div class="speak">';
    html += '    ' + v.content + '';
    html += '</div>';
    if (v.mediaType != undefined && v.mediaType == "image") {
        var pics = v.media.split(',');
        html += '<div class="picbox num_' + pics.length + '">';
        for (var i in pics) {
            var item = pics[i];
            if (item == "") {
                continue;
            }
            html += '<div class="pic"><img src="' + item + '_600x600.jpg" big-src="' + item + '_1000x1000.jpg" sm-src="' + item + '_200x200.jpg"></div>';
        }
        html += '</div>';
    } else if (v.mediaType != undefined && v.mediaType == "video") {
        html += '<div class="videobox"><video class="video"  x5-video-player-type="h5-page" controls="controls" preload="none" src="' + v.media + '"></video></div>';
    }
    if (v.location) {
        html += '<div class="location"><font>' + v.location + '</font></div>';
    }
    html += '';
    html += '<div class="info">';
    html += '    <div class="left">';
    html += '        <span class="create">' + bottlecms.getDateDiff(bottlecms.microtime(v.createTime)) + '</span> ';
    if (v.issuer) html += '        <a class="delete js-del" href="javascript:;"  data-id="' + v.id + '">删除</a>';
    html += '    </div>';
    html += '    <div class="right">';
    html += '        <span class="praise ' + (v.existsPraise == true ? "on" : "") + ' js-praise" data-id="' + v.id + '">' + v.praise + '</span>';
    html += '        <span class="comment js-comment" data-id="' + v.id + '">' + v.comments + '</span>';
    html += '    </div>';
    html += '</div>';
    if ((v.praises && v.praises.length > 0) || (v.commentList && v.commentList.length > 0)) {
        html += '<div class="comments_or_praises">';
        if (v.praises && v.praises.length > 0) {
            html += '<div class="praises"><i class="icon-heart"></i> ';
            for (var o in v.praises) {
                var praise = v.praises[o];
                if (praise) html += '<a class="nick" href="/single/info/' + praise.userId + '"><img class="photo" src="' + praise.photo + '_50x50.jpg">' + '</a>';
                // if(v.praises.length >o+1)html+="，";
            }
            html += '</div>';
        }
        if (v.praises && v.commentList && v.praises.length > 0 && v.commentList.length > 0) {
            html += '<div class="hr"></div>';
        }
        if (v.commentList && v.commentList.length > 0) {
            html += '<div class="comments">';
            for (var o in v.commentList) {
                var comment = v.commentList[o];
                if (comment) html += '<div class="comment-item "><a class="nick" href="/single/info/' + comment.userId + '">' + comment.nickname + '：</a>' + comment.content + '</div>';
            }
            html += '</div>';
        }
        html += '</div>';
    }
    html += '</div>';
    return html;
}


// 点赞
$(".moments_list").on('click', '.item .js-praise', function () {
    var _this = this;
    var moment_id = $(_this).attr('data-id');
    if ($(_this).hasClass('on')) {
        $(_this).removeClass('on');
        bottlecms.get('/m/ajax/momen/unpraise/' + moment_id, null, function (data) {
            if (data.success == true) {
                $(_this).text(bottlecms.parseInt($(_this).text()) - 1)
                bottlecms.toast(data.msg);
            } else {
                $(_this).toggleClass('on');
                bottlecms.toast(data.msg);
            }
        }, function () {
            $(_this).toggleClass('on');
        });
    } else {
        $(_this).addClass('on');
        bottlecms.get('/m/ajax/momen/praise/' + moment_id, null, function (data) {
            if (data.success == true) {
                $(_this).text(bottlecms.parseInt($(_this).text()) + 1)
                bottlecms.toast(data.msg);
            } else {
                $(_this).toggleClass('on');
                bottlecms.toast(data.msg);
            }
        }, function () {
            $(_this).toggleClass('on');
        });
    }
})

function sumitcomment(moment_id, content, rfun) {
    bottlecms.post('/m/ajax/momen/comment',
        {
            momentId:moment_id,
            content: content
        }, function (data) {
            if (data.success == true) {
                if (rfun) return rfun(data);
            } else {
                bottlecms.toast(data.msg);
            }
        });
}


//点击弹出评论框
$(_page).on('click', '.moments_list .item .js-comment', function() {
    var _this = this;
    var moment_id = $(_this).attr('data-id');
    var html = '<div class="set_comment">';
    html += '    <div class="textbox">';
    html += '        <textarea class="" name="content" placeholder="输入评论内容……"></textarea>';
    html += '    </div>';
    html += '    <div class="foot">';
    html += '        <div class="left"></div>';
    html += '        <div class="btnbox"><button class="submit btn ">提交</button></div>';
    html += '    </div>';
    html += '</div>';

    var popup_modal = bottlecms.modal({content:'<div class="write_comment_pop">' + html + '</div>',type:'center'});

    $(popup_modal.thispop).find('textarea[name=content]').focus();

    // 提交评论
    $(popup_modal.thispop).on('click', '.set_comment .submit', function() {
        var set_comment = $(this).parents(".set_comment");
        var content = set_comment.find("textarea[name=content]").val();

        window.console.log(content);
        if (content.length < 1) return bottlecms.toast("请填写内容");

        sumitcomment(moment_id,content,function(data){
            bottlecms.toast(data.msg);
            $(_page).find(".js-comment[data-id='" + moment_id + "']").text(bottlecms.parseInt($(".js-comment[data-id='" + moment_id + "']").text()) + 1)
            popup_modal.close();
        })

    });
});

// 删除
$(document).on('click', '.moments_list .item .js-del', function () {
    var _this = this;
    var moment_id = $(_this).attr('data-id');
    bottlecms.confirm("确定要删除吗？", function () {
        bottlecms.get('/m/ajax/momen/delete/' + moment_id, null, function (data) {
            if (data.success == true) {
                bottlecms.toast(data.msg);
                $(_this).parents('.item').remove();
            } else {
                bottlecms.toast(data.msg);
            }
        });
    })
});

$(function () {
    $('.moments_list ').on('click', '.item .picbox .pic', function () {
        var this_ = $(this);
        var images = this_.parents('.picbox').find('img');
        var imagesArr = new Array();
        $.each(images, function (i, image) {
            imagesArr.push($(image).attr('big-src'));
        });
        $.pictureViewer({
            images: imagesArr, //需要查看的图片，数据类型为数组
            initImageIndex: this_.index() + 1, //初始查看第几张图片，默认1
            scrollSwitch: true //是否使用鼠标滚轮切换图片，默认false
        });
    });
});
